<?php
/**
 * @author guoxiaoyong
 * 索真接口
 */
namespace api\controllers;

use bc\controller\ApiController;
use bc\model\wx\report\CodeModel;
use bc\util\Util;
use bc\api\SzApi;
use bc\util\Sms;
use bc\model\order\report\ShareModel;

class SpokesmanController extends SpokesmanbaseController
{
	

	//登录
	public function actionLogin()
	{
		$moblie = $this->post('moblie','');
		$code = $this->post('code','');
		$code_id = $this->post('code_id','');
		$this->login($moblie,$code,$code_id);
	}

	//登录token
	public function actionLoginbytoken()
	{
		$token = $this->post('token','');
		$this->echoJosnpData(0,['token'=>$token],'成功');
	}

	//注册
	public function actionRegister()
	{
		$moblie = $this->post('moblie','');
		$code = $this->post('code','');
		$code_id = $this->post('code_id','');
		$this->register($moblie,$code,$code_id);
	}

    //登出logout
	public function actionLogout()
	{
		$token = $this->post('token','');
		$this->logout($token);
	}

	//获取验证码
	public function actionGetcode()
	{
		$mobile = $this->post ( 'mobile' );
		$model = new \bc\model\wx\ReportModel ();
		$params ['mobile'] = $mobile;
		// 13651886355

		// 创建一个新的验证码
		$arr = [ ];

		for($i = 0; $i < 4; $i ++)
		{
			$rand = rand ( 0, 9 );
			$arr [] = $rand;
		}

		$code = implode ( '', $arr );
		$data ['code'] = $code;
		$data ['mobile'] = $mobile;
		$data ['create_time'] = time ();

		$codeModel = new CodeModel();
		$codeModel->sort = 'id DESC';
		$code_row = $codeModel->getList ( [
			'mobile' => $mobile
		] );
		$code_id=$codeModel->add ( $data );

		// 发送短信验证码
		$sms_msg = '验证码' . $code . '，本次验证码120s内有效';

		$sms = new Sms ();
		$sms->setMobile ( $mobile );
		$sms->setText ( $sms_msg );
		$result = $sms->send ();
		$msg ['result'] = $result;
		$this->echoJosnpData(0,['code_id'=>$code_id],'验证码已发送!');
	}
}

